/**
 * 通知服务模块
 * 统一管理页面通知和消息提示
 */
class NotificationService {
    constructor() {
        this.container = this.createNotificationContainer();
        this.defaultDuration = 3000;
    }

    /**
     * 创建通知容器
     */
    createNotificationContainer() {
        let container = document.getElementById('notification-container');
        if (!container) {
            container = document.createElement('div');
            container.id = 'notification-container';
            container.className = 'fixed top-4 right-4 z-50 space-y-2';
            document.body.appendChild(container);
        }
        return container;
    }

    /**
     * 显示成功通知
     */
    success(message, duration = this.defaultDuration) {
        return this.show(message, 'success', duration);
    }

    /**
     * 显示错误通知
     */
    error(message, duration = this.defaultDuration) {
        return this.show(message, 'error', duration);
    }

    /**
     * 显示信息通知
     */
    info(message, duration = this.defaultDuration) {
        return this.show(message, 'info', duration);
    }

    /**
     * 显示警告通知
     */
    warning(message, duration = this.defaultDuration) {
        return this.show(message, 'warning', duration);
    }

    /**
     * 显示通知
     */
    show(message, type = 'info', duration = this.defaultDuration) {
        console.log(`NotificationService.show() 被调用: type=${type}, message=${message}`);
        
        const notification = this.createNotification(message, type);
        this.container.appendChild(notification);

        // 添加动画效果
        setTimeout(() => {
            notification.classList.add('translate-x-0');
            notification.classList.remove('translate-x-full');
        }, 10);

        // 自动移除
        if (duration > 0) {
            setTimeout(() => {
                this.remove(notification);
            }, duration);
        }

        return notification;
    }

    /**
     * 创建通知元素
     */
    createNotification(message, type) {
        console.log(`NotificationService.createNotification() 被调用: type=${type}`);
        const styles = this.getTypeStyles(type);
        console.log(`通知样式: ${styles}`);
        
        const notification = document.createElement('div');
        notification.className = `notification transform transition-all duration-300 translate-x-full max-w-sm p-4 rounded-lg shadow-lg flex items-center space-x-3 ${styles}`;
        
        const icon = this.getIcon(type);
        const iconElement = document.createElement('i');
        iconElement.className = `${icon} text-lg`;
        
        const messageElement = document.createElement('div');
        messageElement.className = 'flex-1';
        messageElement.textContent = message;
        
        const closeElement = document.createElement('button');
        closeElement.className = 'text-current opacity-70 hover:opacity-100';
        closeElement.innerHTML = '<i class="fas fa-times"></i>';
        closeElement.onclick = () => this.remove(notification);
        
        notification.appendChild(iconElement);
        notification.appendChild(messageElement);
        notification.appendChild(closeElement);
        
        console.log('通知元素已创建:', notification);
        return notification;
    }

    /**
     * 获取类型样式
     */
    getTypeStyles(type) {
        const styles = {
            success: 'bg-green-500 text-white',
            error: 'bg-red-500 text-white',
            warning: 'bg-yellow-500 text-white',
            info: 'bg-blue-500 text-white'
        };
        return styles[type] || styles.info;
    }

    /**
     * 获取图标
     */
    getIcon(type) {
        const icons = {
            success: 'fas fa-check-circle',
            error: 'fas fa-exclamation-circle',
            warning: 'fas fa-exclamation-triangle',
            info: 'fas fa-info-circle'
        };
        return icons[type] || icons.info;
    }

    /**
     * 移除通知
     */
    remove(notification) {
        if (notification && notification.parentNode) {
            notification.classList.add('translate-x-full');
            notification.classList.remove('translate-x-0');
            
            setTimeout(() => {
                if (notification.parentNode) {
                    notification.parentNode.removeChild(notification);
                }
            }, 300);
        }
    }

    /**
     * 清除所有通知
     */
    clear() {
        this.container.innerHTML = '';
    }

    /**
     * 显示加载中提示
     */
    loading(message = '加载中...') {
        return this.show(message, 'loading', 0);
    }
}

// 创建全局实例
const notificationService = new NotificationService();

// 导出为全局变量
window.NotificationService = NotificationService;
window.notificationService = notificationService;